home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / overview / dtscpluslibrary / sources / process.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  6.4 KB  |  272 lines

  1. /*
  2.     File:        Process.cp
  3.  
  4.     Contains:    TProcess is a Process Manager class.
  5.                   Process.cp contains the class body information for the TProcess class member functions.
  6.  
  7.  
  8.     Written by: Kent Sandvik    
  9.  
  10.     Copyright:    Copyright Â© 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  11.  
  12.                 You may incorporate this Apple sample source code into your program(s) without
  13.                 restriction. This Apple sample source code has been provided "AS IS" and the
  14.                 responsibility for its operation is yours. You are not permitted to redistribute
  15.                 this Apple sample source code as "Apple sample source code" after having made
  16.                 changes. If you're going to re-distribute the source, we require that you make
  17.                 it clear in the source that the code was descended from Apple sample source
  18.                 code, but that you've made changes.
  19.  
  20.     Change History (most recent first):
  21.                 8/18/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  22.                 
  23.  
  24. */
  25. // Include files
  26. #ifndef _PROCESS_
  27. #include "Process.h"
  28. #endif
  29.  
  30.  
  31. // _________________________________________________________________________________________________________ //
  32. // TProcess class member function implementations
  33.  
  34. //    CONSTRUCTORS & DESTRUCTORS
  35. #pragma segment Process                       
  36. TProcess::TProcess(ProcessSerialNumber theNum)
  37. // Default constructor.
  38. {
  39.     fFirstTime = true;                            // signal that we are inside constructor
  40.     fLast = false;
  41.     fProcessID.highLongOfPSN = theNum.highLongOfPSN;
  42.     fProcessID.lowLongOfPSN = theNum.lowLongOfPSN;
  43.  
  44.     fError = ::GetCurrentProcess(&fMyProcessID);// always get our own PSN
  45.     VASSERT(fError == noErr, ("pProblems with GetCurrentProcess() = %d", fError));
  46.  
  47.     fState = this->IProcess();                    // initialize the object
  48.  
  49.     fFirstPSN = fProcessID;                        // define the first one
  50.     fFirstTime = false;                            // signal this time is over
  51. }
  52.  
  53.  
  54. #pragma segment Process
  55. TProcess::~TProcess()
  56. // Destructor, we are not doing anything inside this one just now.
  57. {
  58. }
  59.  
  60.  
  61. //    INITIATION ROUTINES
  62. #pragma segment Process
  63. Boolean TProcess::IProcess()
  64. // We are using a special IProcess member function for initializing class fields to
  65. // known values.
  66. {
  67.     // get our own PSN number
  68.     if (fFirstTime)                                // our first PSN (our own)
  69.     {
  70.         fError = ::GetNextProcess(&fProcessID);
  71.         VASSERT(fError == noErr, ("Problems with GetNextProcess() = %d", fError));
  72.  
  73.         if (fError != noErr)
  74.             goto IProcessFalse;
  75.  
  76.         goto IProcessOK;
  77.     }
  78.     else
  79.     {
  80.         fError = ::GetNextProcess(&fProcessID);    // fetch other PSNs
  81.  
  82.         if (fError != noErr)
  83.         {
  84.             fLast = true;
  85.             this->First();
  86.             goto IProcessFalse;
  87.         }
  88.         else
  89.             goto IProcessOK;
  90.     }
  91. IProcessFalse:return false;
  92. IProcessOK:return true;
  93. }
  94.  
  95.  
  96. //     MAIN INTERFACE
  97. #pragma segment Process
  98. Boolean TProcess::KillApplication(ProcessSerialNumber* thePSN)
  99. // quit the application which is defined by the PSN
  100. {
  101.     AEAddressDesc target;
  102.     AppleEvent theAE,  theAEReply;
  103.  
  104.     theAE.dataHandle = theAEReply.dataHandle = target.dataHandle = NULL;
  105.  
  106.     fError = ::AECreateDesc(typeProcessSerialNumber, (Ptr)thePSN, sizeof(ProcessSerialNumber), &target);
  107.     VASSERT(fError == noErr, ("Problems with AECreateDesc() = %d", fError));
  108.  
  109.     if (fError != noErr)
  110.         goto KillApplicationFalse;
  111.  
  112.     fError = ::AECreateAppleEvent(kCoreEventClass, kAEQuitApplication, &target, kAutoGenerateReturnID, kAnyTransactionID, &theAE);
  113.     VASSERT(fError == noErr, ("Problems with AECreateAppleEvent() = %d", fError));
  114.  
  115.     if (fError != noErr)
  116.     {
  117.         ::AEDisposeDesc(&target);
  118.         goto KillApplicationFalse;
  119.     }
  120.  
  121.     fError = ::AESend(&theAE, &theAEReply, kAENoReply, kAENormalPriority, kNoTimeOut, NULL, NULL);
  122.     VASSERT(fError == noErr, ("Problems with AESend() = %d", fError));
  123.  
  124.     ::AEDisposeDesc(&target);
  125.     ::AEDisposeDesc(&theAE);
  126.  
  127.     if (fError != noErr)
  128.         goto KillApplicationFalse;
  129.     else
  130.         goto KillApplicationOK;
  131.  
  132.  
  133. KillApplicationFalse:return false;
  134. KillApplicationOK:return true;
  135. }
  136.  
  137.  
  138. #pragma segment Process
  139. short TProcess::GetNumProcesses()
  140. // Get the amount of currently running processes.
  141. {
  142.     ProcessSerialNumber aPSN;
  143.     short num = 0;
  144.  
  145.     aPSN.highLongOfPSN = 0;
  146.     aPSN.lowLongOfPSN = kNoProcess;
  147.  
  148.     while (GetNextProcess(&aPSN) == noErr)
  149.         num++;
  150.  
  151.     return num;
  152. }
  153.  
  154.  
  155. #pragma segment Process
  156. ProcessInfoRec TProcess::GetProcessInfoRec()
  157. // Return the full ProcInfoRec of specified process, NULL if we got into trouble.
  158. {
  159.     ProcessInfoRec theRec;
  160.  
  161.     theRec.processName = NULL;
  162.     theRec.processAppSpec = NULL;
  163.     theRec.processInfoLength = sizeof(theRec);
  164.  
  165.     fError = ::GetProcessInformation(&fProcessID, &theRec);
  166.     VASSERT(fError == noErr, ("Problems with GetProcessInformation() = %d", fError));
  167.  
  168.     return theRec;
  169. }
  170.  
  171.  
  172. #pragma segment Process
  173. unsigned long TProcess::GetProcessSize()
  174. // Return size of process.
  175. {
  176.     ProcessInfoRec temp = this->GetProcessInfoRec();
  177.     return temp.processSize;
  178. }
  179.  
  180.  
  181. #pragma segment Process
  182. unsigned long TProcess::GetFreeMem()
  183. // Return the amount of free memory available for the process
  184. {
  185.     ProcessInfoRec temp = this->GetProcessInfoRec();
  186.     return temp.processFreeMem;
  187. }
  188.  
  189.  
  190. #pragma segment Process
  191. unsigned long TProcess::GetLaunchDate()
  192. // Return in seconds the point when the application was launched.
  193. {
  194.     ProcessInfoRec temp = this->GetProcessInfoRec();
  195.     return temp.processLaunchDate;
  196. }
  197.  
  198.  
  199. #pragma segment Process
  200. Boolean TProcess::FindProcess(OSType signature)
  201. // Find process with the right signature, style 'MACS'.
  202. {
  203.     ProcessInfoRec theRec;
  204.     fProcessID.highLongOfPSN = 0;
  205.     fProcessID.lowLongOfPSN = kNoProcess;        // start from beginning
  206.  
  207.     theRec.processName = NULL;
  208.     theRec.processAppSpec = NULL;
  209.     theRec.processInfoLength = sizeof(theRec);
  210.  
  211.     while (::GetNextProcess(&fProcessID) == noErr)
  212.     {
  213.         if (::GetProcessInformation(&fProcessID, &theRec) == noErr)
  214.         {
  215.             if (theRec.processSignature == signature)
  216.                 goto FindProcessOK;                // we found it
  217.         }
  218.     }
  219.     goto FindProcessFalse;                        // we didn't find the process…
  220.  
  221. FindProcessFalse:return false;
  222. FindProcessOK:return true;
  223. }
  224.  
  225.  
  226. //    PUBLIC ACCESSORS AND MUTATORS            
  227.  
  228. #pragma segment Process                       
  229. ProcessSerialNumber TProcess::GetMyProcessID() const
  230. {
  231.     return fMyProcessID;
  232. }
  233.  
  234.  
  235. #pragma segment Process                       
  236. ProcessSerialNumber TProcess::GetProcessID() const
  237. {
  238.     return fProcessID;
  239. }
  240.  
  241.  
  242. // ITERATORS
  243.  
  244. #pragma segment Process                       
  245. void TProcess::Next()
  246. {
  247.     this->IProcess();
  248. }
  249.  
  250.  
  251. #pragma segment Process                       
  252. Boolean TProcess::Last()
  253. {
  254.     return fLast;
  255. }
  256.  
  257.  
  258. #pragma segment Process                       
  259. void TProcess::First()
  260. {
  261.     fProcessID = fFirstPSN;
  262. }
  263.  
  264.  
  265. // _________________________________________________________________________________________________________ //
  266.  
  267. /*    Change History (most recent last):
  268.   No        Init.    Date        Comment
  269.   1            khs        6/10/92        New file
  270.   2            khs        7/6/92        First decent working class
  271. */
  272.